package com.yao.component.circle;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yao.component.circle.dataobject.CircleProfile;
import com.yao.component.circle.dataobject.Message;
import com.yao.component.circle.dataobject.Post;
import com.yao.sdk.net.NetConstant;
import com.yao.sdk.net.NetWork;
import com.yao.sdk.net.ResponseInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/1.
 */
public class DefaultCircleDataSourceImpl extends AbstractCircleDataSource {

    @Override
    public CircleProfile getProfile() {
        return CircleProfile.getInstance();
    }

    public DefaultCircleDataSourceImpl() {

    }


    @Override
    public void requestPublishMessage(Message toBePublishedMessage) {
        String url = NetConstant.URL_CIRCLE_PUBLISH_MESSAGE;
        Log.d("test", "requestPublishMessage--->" + url);
        Map<String, String> params = new HashMap<>();
        params.put(NetConstant.KEY_POST_ID, toBePublishedMessage.getPostId());
        params.put(NetConstant.KEY_MESSAGE_CONTENT, toBePublishedMessage.getContent());
        params.put(NetConstant.KEY_FROM_CIRCLE_USER_ID, toBePublishedMessage.getFromCircleUserId() );
        params.put(NetConstant.KEY_TO_CIRCLE_USER_ID, toBePublishedMessage.getToCircleUserId());
        params.put(NetConstant.KEY_MESSAGE_TYPE, toBePublishedMessage.getMessageType() + "");
        NetWork.request(this, url, params);
    }

    @Override
    public void requestDeleteMessage(String messageId) {
        String url = NetConstant.URL_CIRCLE_DELETE_MESSAGE;
        Log.d("test", "requestDeleteMessage--->" + url);
        Map<String, String> params = new HashMap<>();
        params.put(NetConstant.KEY_MESSAGE_ID, messageId);
        NetWork.request(this, url, params);
    }

    @Override
    public void requestDeletePost(String postId) {
        String url = NetConstant.URL_CIRCLE_DELETE_POST;
        Log.d("test", "requestDeletePost--->" + url);
        Map<String, String> params = new HashMap<>();
        params.put(NetConstant.KEY_POST_ID, postId);
        NetWork.request(this, url, params);

    }


    @Override
    public void requestGetPost() {
        String url = NetConstant.URL_CIRCLE_POST;
        Log.d("test", "requestGetPost--->" + url);
        Map<String, String> params = new HashMap<>();
        params.put(NetConstant.KEY_FIELD1_ID, CircleProfile.getInstance().getField1());
        NetWork.request(this, url, params);

    }


    @Override
    public List<Post> getCachedPost() {
        return dataList;
    }



    @Override
    public void onResponse(String url, ResponseInfo responseInfo) {
        super.onResponse(url, responseInfo);
        if (responseInfo.isOk) {
            switch (url) {
                case  NetConstant.URL_CIRCLE_POST:
                    JSONArray resultArray = JSON.parseObject(responseInfo.content).getJSONObject("result").getJSONArray("postList");
                    dataList = JSON.parseObject(resultArray.toJSONString(), new TypeReference<List<Post>>() {}.getType());
                    circleView.refresh();

                    break;
                case NetConstant.URL_CIRCLE_PUBLISH_MESSAGE:
                    requestGetPost();
                    break;
                case NetConstant.URL_CIRCLE_DELETE_MESSAGE:
                    requestGetPost();
                    break;
                case NetConstant.URL_CIRCLE_DELETE_POST:
                    requestGetPost();
                    break;
            }
        }
    }
}
